<!--drop-down组件参数-->
<!--1. options: 传入的下拉菜单组要数据，和elm数据格式保持同步-->
<!--2. v-model: 绑定值: 单选为字符串类型;多选为数组类型-->
<!--3. placeholder: 占位字符串。默认“请选择”-->
<!--4. change: 方法事件，选中值变化时触发，回调参数默认为当前选中的值-->
<!--5. multiple: 布尔值确定是否单多选-->
<!--6. clearable: 布尔值确定是否可清空(5.16新增)-->

<template>
  <div class="drop-down" v-clickoutside="handleClickOutSide">
    <div class="outside-box" @click="toggle" @mouseover="hideLabelIcon" @mouseout="showLabelIcon">
      <span class="label-value" v-if="labelValue && !isMultipleFlag">{{labelValue}}</span>
      <span class="label-value" v-if="isMultipleFlag && multipleLabel.length !== 0">
        {{multipleLabel[0]}}
        <span
          class="label-num"
          v-show="multipleLabel.length > 1"
        >(+{{multipleLabel.length - 1}})</span>
      </span>
      <span
        class="label-tips"
        v-show="(!labelValue && !isMultipleFlag) || (isMultipleFlag && multipleLabel.length === 0)"
      >{{placeholder}}</span>
      <i class="el-icon-circle-close" v-show="clearable&&isShowClose" @click.stop="resetLabelValue"></i>
      <i
        class="label-icon"
        :class="iconFlag ? 'el-icon-arrow-up' : 'el-icon-arrow-left'"
        v-show="isShowIcon"
      ></i>
    </div>
    <transition name="el-zoom-in-top">
      <ul class="inside-box" v-show="iconFlag">
        <el-scrollbar style="height: 100%;">
          <li v-for="(item, index) in options" :key="index" @click="selectItem(item)">
            <span
              class="item-label"
              :class="{'active-item': labelValue === item.label}"
              v-if="!isMultipleFlag"
            >{{item.label}}</span>
            <div style="position: relative" v-else>
              <span
                class="item-label"
                :class="{'active-item': realMultipleValue.indexOf(item.value) !== -1}"
              >{{item.label}}</span>
              <i class="el-icon-check" v-show="realMultipleValue.indexOf(item.value) !== -1"></i>
            </div>
          </li>
          <div class="no-data-block" v-if="options.length === 0">
            <p>No Data</p>
          </div>
        </el-scrollbar>
        <div class="icon-arrow"></div>
      </ul>
    </transition>
  </div>
</template>
<script>
import clickoutside from "./clickoutside";

export default {
  name: "drop-down",
  data() {
    return {
      isShowClose: false,
      isShowIcon: true,
      iconFlag: false,
      labelValue: "",
      realValue: "",
      multipleLabel: [], // 多选label数组
      realMultipleValue: [], // 多选真实value数组
      isMultipleFlag: false // 是否多选模式flag
    };
  },
  methods: {
    toggle() {
      this.iconFlag = !this.iconFlag;
    },
    selectItem(item) {
      if (this.isMultipleFlag) {
        // 多选模式
        if (this.multipleLabel.indexOf(item.label) === -1) {
          this.multipleLabel.push(item.label);
          this.realMultipleValue.push(item.value);
        } else {
          this.multipleLabel.splice(this.multipleLabel.indexOf(item.label), 1);
          this.realMultipleValue.splice(
            this.realMultipleValue.indexOf(item.value),
            1
          );
        }
        this.$emit("input", this.realMultipleValue);
        this.$emit("change", this.realMultipleValue);
      } else {
        // 单选模式
        this.labelValue = item.label;
        this.realValue = item.value;
        this.$emit("input", this.realValue);
        this.$emit("change", this.realValue);
        this.iconFlag = false;
      }
    },
    hideLabelIcon() {
      if (
        this.labelValue ||
        (this.isMultipleFlag && this.realMultipleValue.length !== 0)
      ) {
        if (this.clearable) this.isShowIcon = false;
        this.isShowClose = true;
      }
    },
    showLabelIcon() {
      if (
        this.labelValue ||
        (this.isMultipleFlag && this.realMultipleValue.length !== 0)
      ) {
        if (this.clearable) this.isShowIcon = true;
        this.isShowClose = false;
      }
    },
    resetLabelValue() {
      //                this.$emit('input', '')
      //                this.$emit('change', '')
      if (this.multiple) {
        this.realMultipleValue = [];
        this.$emit("input", this.realMultipleValue);
        this.$emit("change", this.realMultipleValue);
      } else {
        this.realValue = "";
        this.$emit("input", this.realValue);
        this.$emit("change", this.realValue);
      }
      this.labelValue = "";
      this.multipleLabel = [];
      this.isShowIcon = true;
      this.isShowClose = false;
      this.iconFlag = false;
    },
    handleClickOutSide() {
      this.iconFlag = false;
    }
  },
  props: {
    options: Array,
    clearable: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: "Select"
    },
    multiple: {
      type: Boolean,
      default: false
    },
    value: [Array, String] // 用于构成双向数据流绑定；不用暴漏到外部接口。默认就是叫 value 规定死，父的绑定值就自动初始化传进来了
  },
  watch: {
    //            realValue: function (nVal, oVal) {
    //                this.$emit('input', nVal)
    //                this.$emit('change', nVal)
    //            },
    //            realMultipleValue: function (nVal, oVal) {
    //                this.$emit('input', nVal)
    //                this.$emit('change', nVal)
    //            },
    options: function(nVal, oVal) {
      // console.log('options变化了', nVal, oVal)
      // 不深度观察，目的就是为了初始化的时候获得对应label值，解决异步问题导致的获取不到的bug
      // console.log(this.options, '钱')
      // console.log(this.multiple, '中')
      // console.log(this.value, '后')
      if (this.multiple) {
        // 多选
        if (this.value.length !== 0) {
          this.realMultipleValue = this.value;
          this.multipleLabel = [];
          // 多选有可能有多个初始值，故需处理如下；单选就不需要。
          nVal.forEach(item => {
            this.realMultipleValue.forEach(item2 => {
              if (item2 === item.value) {
                this.multipleLabel.push(item.label);
              }
            });
          });
          // this.multipleLabel = this.value
        }
      } else {
        this.realValue = this.value;
        //                    this.labelValue = this.value
        //9.3修改，否则再异步获取options后labelValue显示的是value值
        nVal.forEach(item => {
          if (this.value === item.value) {
            this.labelValue = item.label;
          }
        });
      }
    },
    value: function(nVal, oVal) {
      if (nVal.length === 0 && nVal !== oVal) {
        if (this.multiple) {
          this.multipleLabel = [];
          this.realMultipleValue = [];
        } else {
          this.realValue = "";
          this.labelValue = "";
        }
      } else {
        // 赋显示值
        if (this.multiple) {
          this.realMultipleValue = this.value;
          this.multipleLabel = [];
          // 多选有可能有多个初始值，故需处理如下；单选就不需要。
          this.options.forEach(item => {
            this.realMultipleValue.forEach(item2 => {
              if (item2 === item.value) {
                this.multipleLabel.push(item.label);
              }
            });
          });
        } else {
          this.realValue = this.value;
          this.options.forEach(item => {
            if (item.value === this.realValue) {
              this.labelValue = item.label;
            }
          });
        }
      }
    }
  },
  directives: {
    clickoutside: clickoutside
  },
  created() {
    if (this.multiple) {
      // 开启多选
      this.isMultipleFlag = true;
      // 多选
      if (this.value.length !== 0) {
        this.realMultipleValue = this.value;
        // 多选有可能有多个初始值，故需处理如下；单选就不需要。
        this.options.forEach(item => {
          this.realMultipleValue.forEach(item2 => {
            if (item2 === item.value) {
              this.multipleLabel.push(item.label);
            }
          });
        });
        // this.multipleLabel = this.value
      }
    } else {
      if (this.value) {
        this.realValue = this.value;
        this.options.forEach(item => {
          if (item.value === this.realValue) {
            this.labelValue = item.label;
          }
        });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.drop-down {
  position: relative;
  .outside-box {
    font-family: MicrosoftYaHei;
    font-size: 14px;
    color: #666666;
    height: 20px;
    line-height: 20px;
    cursor: pointer;
    .el-icon-circle-close {
      margin-left: 10px;
      color: #b4bccc;
      &:hover {
        color: #878d99;
      }
    }
    .label-tips {
      color: #c5cbcc;
    }
    .label-icon {
      margin-left: 10px;
    }
    .icon-blue {
      color: #409eff;
      font-size: 12px;
    }
    .el-icon-arrow-up {
      transform: rotate(0);
      transition: transform 0.2s linear;
    }
    .el-icon-arrow-left {
      transform: rotate(-90deg);
      transition: transform 0.2s linear;
    }
  }
  .inside-box {
    position: absolute;
    top: 25px;
    z-index: 1002;
    /*z-index: 999;*/
    list-style: none;
    background-color: #fff;
    margin: 0;
    padding: 6px 0;
    box-sizing: border-box;
    min-width: 200px;
    border: 1px solid #ebeef5;
    border-radius: 5px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    li {
      padding: 0 10px 0 20px;
      color: #606266;
      height: 30px;
      line-height: 30px;
      cursor: pointer;
      box-sizing: border-box;
      &:hover {
        background-color: #f5f7fa;
      }
      .item-label {
        max-width: 160px;
        display: inline-block;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
    .icon-arrow {
      position: absolute;
      top: -5px;
      left: 20px;
      width: 0;
      height: 0;
      border-width: 0 5px 5px;
      border-style: solid;
      border-color: transparent transparent #fff; /*透明 透明  白*/
    }
    .active-item {
      color: #409eff;
      font-weight: bold;
    }
    .el-icon-check {
      position: absolute;
      top: 8px;
      right: 0;
      color: #409eff;
      font-weight: bold;
      font-size: 12px;
    }
    .no-data-block {
      padding: 20px;
      p {
        text-align: center;
        margin: 0;
        padding: 0;
        color: #c0c4cc;
      }
    }
  }
}
</style>
<style lang="scss">
.drop-down {
  .el-scrollbar__wrap {
    max-height: 275px;
  }
}
</style>


